package com.test.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.test.dao.CarDao;
import com.test.entity.Car;
import com.test.enums.ResultEnum;
import com.test.exception.CarException;
import com.test.model.CarCriteria;
import com.test.service.CarService;
import com.ucar.common.utils.page.jpa.PageImpl;
import com.ucar.common.utils.page.jpa.PagingUtil;
import com.ucar.common.utils.page.jpa.model.PagingConditions;



@Service
public class CarServiceImpl implements CarService {

	@Autowired
	CarDao carDao;
	
	@Override
	public PageImpl<Car> findCarInfosByPage(CarCriteria carCriteria, PagingConditions paging) {
		Page<Car> carInfosPage = carDao.findAll(carCriteria, PagingUtil.getPageable(paging));
		return new PageImpl<>(carInfosPage.getContent(), carInfosPage.getTotalElements());
	}

	@Override
	public Car findOne(Integer id) throws Exception {
		Car car = carDao.findById(id);
		if(car.getVechileNo().length()<5) {
			throw new CarException(ResultEnum.CARNUMBERTOMIN);
		}else if(car.getVechileNo().length()>8){
			throw new CarException(ResultEnum.CARNUMBERTOMAX);
		}else {
			return car;
		}
	}

}
